/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Dom解析XML {

    public static void parseXml(String path) {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder dombuilder = builderFactory.newDocumentBuilder();
            Document document = dombuilder.newDocument();
            Element root = document.createElement("persons");
            document.appendChild(root);

            // 第一个人
            Element person1 = document.createElement("person");
            person1.setAttribute("name", "Linux");
            root.appendChild(person1);

            Element sex = document.createElement("sex");
            sex.appendChild(document.createTextNode("女"));
            person1.appendChild(sex);

            Element address = document.createElement("address");
            address.appendChild(document.createTextNode("湖北"));
            person1.appendChild(address);

            Element phoneNumber = document.createElement("phone");
            phoneNumber.appendChild(document.createTextNode("155124578**"));
            person1.appendChild(phoneNumber);

            // 第二个人
            Element person2 = document.createElement("person");
            person2.setAttribute("name", "Huhx");
            root.appendChild(person2);

            Element sex2 = document.createElement("sex");
            sex2.appendChild(document.createTextNode("男"));
            person2.appendChild(sex2);

            Element address2 = document.createElement("address");
            address2.appendChild(document.createTextNode("湖南"));
            person2.appendChild(address2);

            Element phoneNumber2 = document.createElement("phone");
            phoneNumber2.appendChild(document.createTextNode("155124578**"));
            person2.appendChild(phoneNumber2);

            // 将上述的document写入到文件中
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(document);
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            PrintWriter pw = new PrintWriter(new FileOutputStream(path));
            StreamResult result = new StreamResult(pw);
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        parseXml("text.xml");
    }
}
